package top.carrotguo.weibo.model;

import com.orhanobut.logger.Logger;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;


import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import top.carrotguo.weibo.bean.CommentResultBean;
import top.carrotguo.weibo.bean.SendCommentBean;
import top.carrotguo.weibo.contract.SendCommentContract;
import top.carrotguo.weibo.retrofit.RetrofitUtil;
import top.carrotguo.weibo.retrofit.api.CommentService;
import top.carrotguo.weibo.retrofit.callback.BaseCallback;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;
import top.carrotguo.weibo.utils.MyConstans;

public class SendCommentModel implements SendCommentContract.Model {

    @Override
    public void sendComment(Oauth2AccessToken token, String blogId,
                            String textContent, BaseCallback<CommentResultBean, ErrorInfo> callback) {
        CommentService service = RetrofitUtil.getInstance().create(CommentService.class);
        Call<CommentResultBean> call = service.sendComment(token.getToken(),textContent,blogId);
        call.enqueue(new Callback<CommentResultBean>() {
            @Override
            public void onResponse(Call<CommentResultBean> call, Response<CommentResultBean> response) {
                CommentResultBean result = response.body();
                if (result != null) {
                    callback.onSuccessed(result);
                } else {
                    callback.onError(new ErrorInfo(MyConstans.REQUEST_LIMIT,"访问次数受限"));
                }
            }

            @Override
            public void onFailure(Call<CommentResultBean> call, Throwable t) {
                Logger.e(t.getMessage());
                callback.onError(new ErrorInfo(MyConstans.CALL_FAILURE,t.getMessage()));
            }
        });


    }
}
